﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform.UC
{
    public partial class InspectionDepartmen : UserControl
    {
        private ComboBox.SubmitUnit _CmbSubmitUnit;

        public InspectionDepartmen()
        {
            InitializeComponent();

            InitControls();

            btnSearch_Click(null, null);
        }

        private void InitControls()
        {
            Static.Methods.CreateDGVColumns(ref dgvSubmitUnit, Model.SystemEnum.DGVType.SubmitUnit);
            dgvSubmitUnit.CellClick += DgvSubmitUnit_CellClick;

            pagDepartment.Event_SearchDataInfo += PagDepartment_Event_SearchDataInfo;
            pagDepartment.Event_UpdatePagingControl += btnSearch_Click;
            
            _CmbSubmitUnit = new ComboBox.SubmitUnit();
            _CmbSubmitUnit.Dock = DockStyle.Fill;
            _CmbSubmitUnit.Visible = true;
            pSubmitUnit.Controls.Add(_CmbSubmitUnit);
        }

        private void DgvSubmitUnit_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;

            if (sender != null && e.RowIndex >= 0)
            {
                if (dgvSubmitUnit.Rows[e.RowIndex].Cells["Selected"].Value != null && (bool)dgvSubmitUnit.Rows[e.RowIndex].Cells["Selected"].Value)
                    dgvSubmitUnit.Rows[e.RowIndex].Cells["Selected"].Value = false;
                else
                    dgvSubmitUnit.Rows[e.RowIndex].Cells["Selected"].Value = true;
            }
        }

        private void PagDepartment_Event_SearchDataInfo(int pageIndex, int pageSize)
        {
            int _StartIndex = (pageIndex * pageSize) + 1;
            int _SearchCount = pageSize;

            Dictionary<string, string> _LogisticList = GetLogisticList();
            List<Model.DB.tb_SubmitUnit> _ListInstrument = new DataAccess.tb_SubmitUnit().GetSubmitUnitList(_StartIndex, _SearchCount, _LogisticList);
            dgvSubmitUnit.DataSource = _ListInstrument;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                int _DataCount;

                Dictionary<string, string> _LogisticList;

                try
                {
                    _LogisticList = GetLogisticList();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "获取查询条件失败，提示信息：" + ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    _CmbSubmitUnit.Focus();
                    return;
                }

                _DataCount = new DataAccess.tb_SubmitUnit().GetSubmitUnitCount(_LogisticList);
                pagDepartment.IniEnabled(_DataCount);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "查询送检单位失败，错误信息：" + ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Forms.InspectionDepartmentForm _InspectionDepartmentForm = new Forms.InspectionDepartmentForm();

            if (_InspectionDepartmentForm.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    new DataAccess.tb_SubmitUnit().SaveSubmitUnit(_InspectionDepartmentForm.SubmitUnit);


                    if (!string.IsNullOrEmpty(_InspectionDepartmentForm.SubmitUnit.ImageInfo))
                    {
                        string _Id = new DataAccess.tb_SubmitUnit().GetSubmitUnitId(_InspectionDepartmentForm.SubmitUnit.Name);
                        if (!string.IsNullOrEmpty(_Id))
                        {
                            Static.Methods.UpdateImage(Model.SystemEnum.ImageType.SubmitUnit, _Id, _InspectionDepartmentForm.SubmitUnit.ImageInfo);
                        }
                    }

                    btnSearch_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("添加送检单位失败，错误信息：" + ex.ToString());
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> _IndexList = new List<string>();
            bool _Select;
            for (int i = 0; i < dgvSubmitUnit.Rows.Count; i++)
            {
                if (dgvSubmitUnit.Rows[i].Cells["Selected"].Value == null) continue;
                _Select = (bool)dgvSubmitUnit.Rows[i].Cells["Selected"].Value;
                if (_Select)
                    _IndexList.Add(dgvSubmitUnit.Rows[i].Cells["Id"].Value.ToString());
            }

            if (_IndexList.Count <= 0)
            {
                MessageBox.Show(this, "请选择送检单位！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (MessageBox.Show("是否确认删除？", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                try
                {
                    new DataAccess.tb_SubmitUnit().DeleteForID(_IndexList);
                    btnSearch_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "删除送检单位失败，错误信息：" + ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (dgvSubmitUnit.SelectedRows.Count <= 0)
            {
                MessageBox.Show(this, "请选择送检单位信息！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }


            Model.DB.tb_SubmitUnit _Temp = new DataAccess.tb_SubmitUnit().GetSubmitUnitForId(dgvSubmitUnit.SelectedRows[0].Cells["Id"].Value.ToString());

            if (_Temp == null)
            {
                MessageBox.Show(this, "送检单位下无相关信息！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            Forms.InspectionDepartmentForm _InspectionDepartmentForm = new Forms.InspectionDepartmentForm();
            _InspectionDepartmentForm.ModifyInfo(_Temp);

            if (_InspectionDepartmentForm.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    _InspectionDepartmentForm.SubmitUnit.Id = _Temp.Id;
                    new DataAccess.tb_SubmitUnit().UpdateSubmitUnit(_InspectionDepartmentForm.SubmitUnit);

                    if (!string.IsNullOrEmpty(_InspectionDepartmentForm.SubmitUnit.ImageInfo))
                    {
                        Static.Methods.UpdateImage(Model.SystemEnum.ImageType.SubmitUnit, _InspectionDepartmentForm.SubmitUnit.Id.ToString(), _InspectionDepartmentForm.SubmitUnit.ImageInfo);
                    }

                    btnSearch_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("修改送检单位失败，错误信息：" + ex.ToString());
                }
            }
        }

        private Dictionary<string, string> GetLogisticList()
        {
            Dictionary<string, string> _Result = new Dictionary<string, string>();

            //_Result.Add("Name", checkName.Checked ? txtName.Text : string.Empty);
            _Result.Add("Name", checkName.Checked ? _CmbSubmitUnit.Text : string.Empty);

            return _Result;
        }
    }
}
